import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import axios from "axios";
import "./assets/css/el-reset.css";
import "./assets/css/base.css";

import subMenu from "qf-sub-menu";

import NProgress from "nprogress";
import "nprogress/nprogress.css";

Vue.use(subMenu);

Vue.prototype.$axios = axios;

Vue.use(ElementUI);

Vue.config.productionTip = false;

//全局前置钩子(路由守卫)
router.beforeEach((to, from, next) => {
  NProgress.start();
  //to 即将进入的路由
  //from 即将离开的路由
  //next 放行
  //判断本地存储中是否有token
  //除了登入页以外,其它所有的页面,在没有登入的情况下都不给用户访问
  //如果有就放行,如果没有就不通过,重新跳转到登入页
  // console.log(to)
  let token = localStorage.getItem("qf-token") || "";
  if (token) {
    //已经登入
    if (store.state.sideMenu.length === 0) {
      store.dispatch("GET_MENULIST").then(() => {
        next({ path: to.path });
      });
    } else {
      next();
    }
  } else {
    //没有登入
    //判断是不是访问的登入页
    if (to.path !== "/login") {
      //跳转到登入页
      next({ path: "/login" });
    } else {
      next();
    }
  }
});

router.afterEach((to, from) => {
  let matched = [...to.matched];
  matched.splice(0, 1), store.commit("SET_BREADCRUMB", matched);
  NProgress.done();
});

new Vue({
  store,
  router,
  render: (h) => h(App),
}).$mount("#app");
